﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace EmlViewer.Behaviours
{
    public static class DoubleClickBehaviour
    {
        #region DataGridDoubleClick (Attached DependencyProperty)
        public static readonly DependencyProperty DataGridDoubleClickProperty =
            DependencyProperty.RegisterAttached("DataGridDoubleClick",
                typeof (ICommand), typeof (DoubleClickBehaviour),
                new PropertyMetadata(OnDataGridDoubleClickChanged));
        public static void SetDataGridDoubleClick(DependencyObject o, double value)
        {
            o.SetValue(DataGridDoubleClickProperty, value);
        }
        public static double GetDataGridDoubleClick(DependencyObject o)
        {
            return (double) o.GetValue(DataGridDoubleClickProperty);
        }
        private static void OnDataGridDoubleClickChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var dg = d as Control;
            if (dg != null)
            {
                var ic = e.NewValue as ICommand;
                if (ic != null)
                {
                    dg.MouseDoubleClick += (s, a) =>
                    {
                        if (ic.CanExecute(a))
                        {
                            ic.Execute(a);
                        }
                    };
                }
            }
        }
        #endregion
    }
}
